package com.qlylkj.lbk.base

import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.forest.core.etx.hideKeyboard
import com.forest.core.etx.startKtxActivity
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.model.event.LiveEvent
import com.qlylkj.lbk.network.NetworkChangeReceiver
import com.qlylkj.lbk.ui.user.login.LoginActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel

/**
 *    author : duanguosen
 *    date   : 2019/11/25
 *    desc   :
 */
abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {

    private val filter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")

    /**
     * 网络状态变化的广播
     */
    private lateinit var mNetworkChangeReceiver: NetworkChangeReceiver

    lateinit var mImmersionBar: ImmersionBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(setLayoutId())
        LiveEventBus.get(LiveEvent.NETWORK_CHANGE_KEY, Boolean::class.java)
            .observeSticky(this, Observer<Boolean> {
                it?.let {
                    PreferenceHelper.hasNetwork = it
                    checkNetwork()
                }
            })
        initStatusBar()
        initView()
        initData()
    }

    @LayoutRes
    protected abstract fun setLayoutId(): Int

    protected abstract fun initView()
    protected abstract fun initData()

    /**
     * 无网状态—>有网状态 的自动重连操作，子类可重写该方法
     */
    open fun doReConnected() {

    }

    open fun doNotNetwork() {

    }

    private fun checkNetwork() {
        if (PreferenceHelper.hasNetwork) {
            doReConnected()
        } else {
            doNotNetwork()
        }
    }

    protected fun toLogin() {
        startKtxActivity<LoginActivity>()
    }

    protected fun isLoginPrompt(): Boolean {
        if (!PreferenceHelper.isLogin) {
            toLogin()
        }
        return PreferenceHelper.isLogin
    }

    open fun initStatusBar() {
        mImmersionBar = ImmersionBar.with(this).keyboardEnable(true)
        mImmersionBar.fitsSystemWindows(true).statusBarColor(R.color.colorPrimary).init()
    }

    private fun registerNetworkChangeReceiver() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mNetworkChangeReceiver = NetworkChangeReceiver()
            registerReceiver(mNetworkChangeReceiver, filter)
            checkNetwork()
        }
    }

    private fun unregisterNetworkChangeReceiver() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            unregisterReceiver(mNetworkChangeReceiver)
        }
    }

    override fun onResume() {
        registerNetworkChangeReceiver()
        super.onResume()
    }

    override fun onPause() {
        unregisterNetworkChangeReceiver()
        super.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()
    }

    override fun finish() {
        if (!isFinishing) {
            hideKeyboard()
            super.finish()
        }
    }

}